﻿using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.WiFiFixatorModels;
using SimpleWifi;
using SimpleWifi.Win32;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DevelopTool.Common
{
    /// <summary>
    /// wifi帮助类
    /// </summary>
    public class WifiHelper
    {
        private Wifi wifi;

        public WifiHelper()
        {
            wifi = new Wifi();
        }

        /// <summary>
        /// 获取WiFi对象集合
        /// </summary>
        /// <returns></returns>
        public List<WiFiFixator> GetWiFiList(bool refreshWiFiList = false)
        {
            if (refreshWiFiList)
                RefreshWiFiList();

            List<WiFiFixator> wiFiFixators = new List<WiFiFixator>();
            var accessPoints = wifi.GetAccessPoints().OrderByDescending(ap => ap.SignalStrength);//按信号强度排序
            foreach (AccessPoint accessPoint in accessPoints)
            {
                wiFiFixators.Add(new WiFiFixator
                {
                    Name = accessPoint.Name,
                    SignalStrength = accessPoint.SignalStrength.ToInt(),
                    HasProfile = accessPoint.HasProfile,
                    IsSecure = accessPoint.IsSecure,
                    IsConnected = accessPoint.IsConnected,
                    ProfileXML = accessPoint.GetProfileXML()
                });
            }
            return wiFiFixators;
        }


        /// <summary>
        /// 根据ssid获取WiFi对象
        /// </summary>
        /// <param name="ssid"></param>
        /// <returns></returns>
        public WiFiFixator GetWiFi(string ssid)
        {
            return GetWiFiList().FirstOrDefault(it => it.Name == ssid);
        }


        /// <summary>
        /// 获取WiFi连接状态
        /// </summary>
        /// <returns></returns>
        public bool WifiConnectedStatus(string ssid = null)
        {
            if (string.IsNullOrEmpty(ssid))
            {
                return wifi.ConnectionStatus == WifiStatus.Connected;
            }
            else
            {
                WiFiFixator wiFiFixator = GetWiFiList().FirstOrDefault(it => it.Name == ssid);
                if (wiFiFixator == null) return false;

                return wiFiFixator.IsConnected;
            }
        }


        /// <summary>
        /// 根据ssid连接WiFi
        /// </summary>
        /// <param name="ssid"></param>
        /// <returns></returns>
        public LogModel ConnectWiFi(string ssid)
        {
            try
            {
                WiFiFixator wiFiFixator = GetWiFiList().FirstOrDefault(it => it.Name == ssid);
                if (wiFiFixator == null) return new LogModel(LogType.Warning, $"监控的WiFi【{ssid}】不在范围内，无法连接");

                if (!wiFiFixator.HasProfile) return new LogModel(LogType.Warning, $"WiFi【{ssid}】不存在网络配置文件，请先在系统右下角网络管理器中连接该WiFi后再监控");

                var accessPoint = wifi.GetAccessPoints().FirstOrDefault(it => it.Name == wiFiFixator.Name);
                if (accessPoint == null) return new LogModel(LogType.Warning, $"监控的WiFi【{ssid}】不在范围内或WiFi列表未刷新，稍后重试");

                AuthRequest authRequest = new AuthRequest(accessPoint);

                if (accessPoint.Connect(authRequest, false))
                {
                    return new LogModel(LogType.Info, $"WiFi【{ssid}】连接成功");
                }
                else
                {
                    return new LogModel(LogType.Warning, $"WiFi【{ssid}】连接失败");
                }
            }
            catch (Exception ex)
            {
                return new LogModel(LogType.Error, $"WiFi【{ssid}】连接异常，{ex.Message}");
            }
        }

        /// <summary>
        /// 获取已连接的WiFi对象
        /// </summary>
        /// <returns></returns>
        public WiFiFixator GetConnectedWiFi()
        {
            return GetWiFiList().FirstOrDefault(it => it.IsConnected == true);
        }


        /// <summary>
        /// 断开WiFi连接
        /// </summary>
        /// <returns></returns>
        public LogModel Disconnect()
        {
            try
            {
                string ssid = string.Empty;
                WiFiFixator wiFiFixator = GetConnectedWiFi();
                if (wiFiFixator != null) ssid = wiFiFixator.Name;

                wifi.Disconnect();
                return new LogModel(LogType.Info, $"已断开WiFi【{ssid}】");
            }
            catch (Exception ex)
            {
                return new LogModel(LogType.Error, ex.Message);
            }
        }

        /// <summary>
        /// 强制让网卡刷新网络列表  这个方法好像不太好使，但是没找到解决办法
        /// </summary>
        private void RefreshWiFiList()
        {
            if (wifi.wlanClient == null || wifi.wlanClient.Interfaces == null) return;

            foreach (WlanInterface wlanIface in wifi.wlanClient.Interfaces)
            {
                wlanIface.Scan();
            }
        }

    }
}
